package com.example.bzh.gamecat.ui.live;

import android.support.v4.app.Fragment;
import android.util.SparseArray;

import com.example.bzh.gamecat.ui.live.attention.AttentionFragment;
import com.example.bzh.gamecat.ui.live.hot.HotFragment;
import com.example.bzh.gamecat.ui.live.newest.NewestFragment;

/**
 * Created by peter on 2016/8/17.
 */
public class FragmentFactory {

    private static SparseArray<Fragment> fragments = new SparseArray<>();

    private static final int ATTENTION = 0;
    private static final int HOT = 1;
    private static final int NEWEST = 2;

    //创建Fragment对象的工厂方法.
    public static Fragment createFragment(int position) {
        //从集合中取值
        Fragment fragment = fragments.get(position);
        if (fragment == null) {
            switch (position) {
                case ATTENTION:
                    fragment = new AttentionFragment();
                    break;
                case HOT:
                    fragment = new HotFragment();
                    break;
                case NEWEST:
                    fragment = new NewestFragment();
                    break;
            }
            //存储Fragment对象
            fragments.put(position, fragment);
        }

        return fragment;
    }
}
